<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of users
 *
 * @author chandu
 */
include CONFIG_PATH . 'errors.php';

class users extends baseController {

    private $users;
    public $verify_modules;
    public $key;
    public $errors;

    public function __construct() {
        parent::__construct();
        $this->errors = new errors();
        $this->users = new users_model();
        //This code is to redirect if already session exists
        if ($_SESSION['email']) {

            $this->verify_modules = $this->users->modules($this->tables->table2, $this->tables->table4);
            $this->show("dashboard/index");

            exit();
            //print_r($verify);
            //  include 'dashboard_controller.php';
            //  $dashboard_controller = new dashboard_controller();
        }
    }

    public function invoke() {
                //This code is to change password
        if (isset($_GET['c_password'])) {

            $this->show("users/change_password");
            if ($_POST) {
                $this->result = $this->users->change_password($this->tables->table1, $_POST['c_pass'], $_POST['n_pass']);
                if ($this->result) {
                    $msg = $this->errors->change_password_sucess;
                    echo ("<SCRIPT LANGUAGE='JavaScript'>
                       window.alert('$msg');
                      </SCRIPT>");
                } else {
                    $msg = $this->errors->default_failed;
                    echo ("<SCRIPT LANGUAGE='JavaScript'>
                       window.alert('$msg');
                      </SCRIPT>");
                }
            }
            exit();
        }
        //This code is to reset password

        if (isset($_GET['e_pass'])) {
            $this->key = $_GET['e_pass'];
            $this->show("users/reset_password");
            if ($_POST) {
                $this->result = $this->users->reset_password($this->tables->table1, $this->key, $_POST['n_pass']);
                if ($this->result) {
                    $msg = $this->errors->reset_pass_successful;
                    echo ("<SCRIPT LANGUAGE='JavaScript'>
                       window.alert('$msg');
                      </SCRIPT>");
                } else {
                    $msg = $this->errors->default_failed;
                    echo ("<SCRIPT LANGUAGE='JavaScript'>
                       window.alert('$msg');
                      </SCRIPT>");
                }
            }
            exit();
        }
          //This code is to activate the account througu email
        if (isset($_GET['key'])) {

            $this->key = $_GET['key'];
            $active = $this->users->activation($this->key, $this->tables->table1);

            if ($active == "activated") {

                $msg = $this->errors->account_already_activated;
                echo ("<SCRIPT LANGUAGE='JavaScript'>
                       window.alert('$msg');
                      </SCRIPT>");
                // header('Location: index.php');
                $this->redirect("index");
            } else {
                //header('Location: index.php');
                $this->redirect("index");
            }
        }
          //This code is for forget password
        if (isset($_GET['f_pass'])) {
            if (isset($_POST['forgot_password'])) {
                $this->data = $_POST['email'];
                $this->result = $this->users->forgot_password($this->tables->table1, $this->data);
            }
            $this->show("users/forgot_password");
            exit();
        }
  //This code is for login
        if (isset($_POST['login'])) {
            $reslt = $this->users->get_login($_POST['email'], $_POST['password'], $this->tables->table1);
if($reslt == "not_activated") {
     $msg = $this->errors->login_not_activated;
     echo ("<SCRIPT LANGUAGE='JavaScript'>
                       window.alert('$msg');
                      </SCRIPT>");
}
            if ($reslt == "ok") {
                $this->verify_modules = $this->users->modules($this->tables->table2, $this->tables->table4);
                $this->show("dashboard/index");
                //include 'dashboard_controller.php';
                //$verify = $this->users->modules();
                // $dashboard_controller = new dashboard_controller();
                exit();
            } else if ($reslt == "failed") {
                $msg = $this->errors->login_failed;
                echo ("<SCRIPT LANGUAGE='JavaScript'>
                       window.alert('$msg');
                      </SCRIPT>");
            }
        }
          //This code is for user registration
        if (isset($_GET['q'])) {

            $this->show("users/register");
            if (isset($_POST['register'])) {
                if (isset($_POST['email']) && isset($_POST['password']) && isset($_POST['schoolname']) && isset($_POST['mobile'])) {
                    $reslt = $this->users->getregister($_POST['email'], $_POST['password'], $_POST['schoolname'], $_POST['mobile'], $this->tables->table1);
                    if ($reslt == "alreadyregistered") {

                        $msg = $this->errors->mail_already_registered;
                        echo ("<SCRIPT LANGUAGE='JavaScript'>
                       window.alert('$msg');
                      </SCRIPT>");
                    } else if ($reslt == "ok") {

                        $msg = $this->errors->registration_sucess;
                        echo ("<SCRIPT LANGUAGE='JavaScript'>
                       window.alert('$msg');
                      </SCRIPT>");

                        //header("location:view/dashboard/index.php");
                    } else if ($reslt == "fail") {
                        $msg = $this->errors->registration_failed;
                        echo ("<SCRIPT LANGUAGE='JavaScript'>
                       window.alert('$msg');
                      </SCRIPT>");
                    }
                }
            }
        } else {
            // include VIEW_PATH . 'users/login.php';
            $this->show("users/login");
            //$this->template->load("header");
        }
    }

}

?>